package sy.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import sy.dao.IotEntranceURIProjectMappingDaoI;
import sy.dao.IotPatrolURIProjectMappingDaoI;
import sy.model.IotEntranceURI;
import sy.model.IotEntranceURIProjectMapping;
import sy.model.IotPatrolURIProjectMapping;
@Repository("iotPatrolURIProjectMappingDao")
public class IotPatrolURIProjectMappingDaoImpl implements IotPatrolURIProjectMappingDaoI{
	private SessionFactory sessionFactory;

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}
	
	@Autowired
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	
	private Session getCurrentSession(){
		return this.getSessionFactory().getCurrentSession();
	}

	@Override
	public void save(IotPatrolURIProjectMapping iotPatrolURIProjectMapping){
		// TODO Auto-generated method stub
		this.getCurrentSession().save(iotPatrolURIProjectMapping);
	}

	@Override
	public void update(IotPatrolURIProjectMapping iotPatrolURIProjectMapping) {
		// TODO Auto-generated method stub
		this.getCurrentSession().saveOrUpdate(iotPatrolURIProjectMapping);
	}

	@Override
	public void delete(List<Integer> ids) {
		// TODO Auto-generated method stub
		String hql = "delete from IotPatrolURIProjectMapping where id in :ids";
		Query query = this.getCurrentSession().createQuery(hql);
		query.setParameterList("ids", ids);
		query.executeUpdate();
	}

	@Override
	public List<IotPatrolURIProjectMapping> list() {
		// TODO Auto-generated method stub
		return this.getCurrentSession().createQuery("from IotPatrolURIProjectMapping").list();
	}

	@Override
	public List<IotPatrolURIProjectMapping> listIotPatrolURIProjectMappingByKey(
			String key) {
		// TODO Auto-generated method stub
		String hql = "from IotPatrolURIProjectMapping where iotPatrolURI.id like :key or sysProject.id like :key";
		Query query = this.getCurrentSession().createQuery(hql);
		query.setString("key", "%"+key+"%");
		return query.list();
	}

	@Override
	public IotPatrolURIProjectMapping getIotPatrolURIProjectMappingById(
			Integer id) {
		// TODO Auto-generated method stub
		String hql = "from IotPatrolURIProjectMapping where id = :id";
		Query query = this.getCurrentSession().createQuery(hql);
		query.setParameter("id", id);
		List<IotPatrolURIProjectMapping> list = query.list();
		if (!list.isEmpty()) {
			return list.get(0);
		}
		return null;
	}
}
